<template>
  <div class="app-container">
    <el-card class="box-card">
    <el-form ref="form" :model="form" :rules="rules" :inline="true">

      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="form.nickName" placeholder="请输入用户昵称" style="width: 100%"/>
      </el-form-item>
      <el-form-item label="投票标题" prop="voteTitile">
        <el-input v-model="form.voteTitile" placeholder="请输入投票标题" style="width: 100%"/>
      </el-form-item>


      <el-form-item label="投票类型" prop="voteType">
        <el-select style="width: 100%" v-model="form.voteType" clearable placeholder="请选择" >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="投票选项" prop="voteOption">
        <el-input v-model="form.voteOption" placeholder="创建投票选项投票才可发布" style="width: 100%" />
      </el-form-item>




      <!--      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投票ID" prop="voteId">
        <el-input
          v-model="queryParams.voteId"
          placeholder="请输入投票ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投票标题" prop="voteTitile">
        <el-input
          v-model="queryParams.voteTitile"
          placeholder="请输入投票标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投票选项" prop="voteOption">
        <el-input
          v-model="queryParams.voteOption"
          placeholder="请输入投票选项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投票人数" prop="optionNum">
        <el-input
          v-model="queryParams.optionNum"
          placeholder="请输入投票人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本项排名" prop="optionPage">
        <el-input
          v-model="queryParams.optionPage"
          placeholder="请输入本项排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注1" prop="note1">
        <el-input
          v-model="queryParams.note1"
          placeholder="请输入备注1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注2" prop="note2">
        <el-input
          v-model="queryParams.note2"
          placeholder="请输入备注2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注3" prop="note3">
        <el-input
          v-model="queryParams.note3"
          placeholder="请输入备注3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注4" prop="note4">
        <el-input
          v-model="queryParams.note4"
          placeholder="请输入备注4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注5" prop="note5">
        <el-input
          v-model="queryParams.note5"
          placeholder="请输入备注5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      -->

    </el-form>
<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"  plain  @click="handleAddd">添加投票选项</el-button>
      </el-col>
    </el-row>-->
    <div style="float:right; margin-bottom: 15px;">
      <el-col :span="1.5">

        <el-button
          v-show="total>0"
          v-if="isShow"
          type="success"
          plain
          size="mini"
          @click="handleUpdateFb()"
          v-hasPermi="['dr:vote:editfb']"
        >发布投票</el-button>
        <el-button
          v-if="isShow1"
          type="danger"
          plain
          size="mini"
          @click="handleUpdateJs"
          v-hasPermi="['dr:vote:editjs']"
        >结束投票</el-button>
        <el-button type="primary" size="mini" v-if="isShow"  plain  @click="handleAddd">添加投票选项</el-button>
      <el-button plain size="mini" @click="gotolink">返 回</el-button>
      </el-col>
    </div>
<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dr:option:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dr:option:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dr:option:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dr:option:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" border :data="optionList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column type="index" label="序号"   :index="indexMethod" align="center"/>
<!--      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
<!--      <el-table-column label="投票ID" align="center" prop="voteId" />-->
      <el-table-column label="投票标题" align="center" prop="voteTitile" />
      <el-table-column label="投票类型" align="center" prop="voteType" >
        <template slot-scope="scope">
          <span v-if="scope.row.voteType==1">单选</span>
          <span v-if="scope.row.voteType==2">多选</span>
        </template>
      </el-table-column>
      <el-table-column label="投票选项" align="center" prop="voteOption" />
<!--      <el-table-column label="投票人数" align="center" prop="optionNum" />
      <el-table-column label="本项排名" align="center" prop="optionPage" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="备注1" align="center" prop="note1" />
      <el-table-column label="备注2" align="center" prop="note2" />
      <el-table-column label="备注3" align="center" prop="note3" />
      <el-table-column label="备注4" align="center" prop="note4" />
      <el-table-column label="备注5" align="center" prop="note5" />-->
      <el-table-column label="操作" align="center" fixed="right"class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--        <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dr:option:edit']"
          >修改</el-button>-->
          <el-button
            v-if="isShow"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dr:option:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改投票选项对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="投票ID" prop="voteId">
          <el-input v-model="form.voteId" placeholder="请输入投票ID" />
        </el-form-item>
        <el-form-item label="投票标题" prop="voteTitile">
          <el-input v-model="form.voteTitile" placeholder="请输入投票标题" />
        </el-form-item>
        <el-form-item label="投票选项" prop="voteOption">
          <el-input v-model="form.voteOption" placeholder="请输入投票选项" />
        </el-form-item>
        <el-form-item label="投票人数" prop="optionNum">
          <el-input v-model="form.optionNum" placeholder="请输入投票人数" />
        </el-form-item>
        <el-form-item label="本项排名" prop="optionPage">
          <el-input v-model="form.optionPage" placeholder="请输入本项排名" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注1" prop="note1">
          <el-input v-model="form.note1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2" prop="note2">
          <el-input v-model="form.note2" placeholder="请输入备注2" />
        </el-form-item>
        <el-form-item label="备注3" prop="note3">
          <el-input v-model="form.note3" placeholder="请输入备注3" />
        </el-form-item>
        <el-form-item label="备注4" prop="note4">
          <el-input v-model="form.note4" placeholder="请输入备注4" />
        </el-form-item>
        <el-form-item label="备注5" prop="note5">
          <el-input v-model="form.note5" placeholder="请输入备注5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import { listOption, getOption, delOption, addOption, updateOption } from "@/api/module/dr/option";
import {getVote, updateVoteFb, updateVoteJs} from "@/api/module/dr/vote";
import watermark from "watermark-dom";
export default {
  name: "Option",
  data() {
    return {
      options: [{
        value: '1',
        label: '单选'
      }, {
        value: '2',
        label: '多选'
      }],
      // 传递过的数组
      carryCurrentRowCode: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票选项表格数据
      optionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //显示
      //isShow:true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        nickName: null,
        voteId: null,
        voteTitile: null,
        voteType: null,
        voteOption: null,
        optionNum: null,
        optionPage: null,
        note: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        voteOption: [{required: true,message: "请先填写投票选项",trigger: "blur"}],}


    };
  },
  created() {

    // 从发布投票界面接收到carryCurrentRowCode数组
    this.carryCurrentRowCode = this.$route.query.carryCurrentRowCode;
    this.queryParams.voteId=this.carryCurrentRowCode.id;
    this.getList();
    this.form.nickName=this.carryCurrentRowCode.nickName;
    this.form.voteTitile=this.carryCurrentRowCode.voteTitile;
    this.form.voteType=this.carryCurrentRowCode.voteType;
    this.form.voteId=this.carryCurrentRowCode.id;
    this.isShow();
    this.isShow1();
    //this.queryParams.voteId=this.carryCurrentRowCode.id
   /* let nn = this.queryParams.voteId;
    let zz = this.carryCurrentRowCode.id;
    console.log(zz+'\\\\\\211212313');
    console.log(nn+'\\\\\\zznnnnnnn');*/
  },
  mounted() {
    watermark.load({ watermark_txt: "  "   });
  },
  methods: {
    indexMethod(index) {
      index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return index
    },
    /** 返回按钮 */
    gotolink(){
      //指定跳转地址
      this.$router.replace("/zonghe/drvote")
    },
    /** 查询投票选项列表 */
    getList() {
      this.loading = true;
      listOption(this.queryParams).then(response => {
        this.optionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form.voteOption =null;
      this.resetForm("voteOption");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投票选项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOption(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投票选项";
      });
    },
    /** 发布投票按钮 */
    handleUpdateFb(){

      this.$confirm('是否发布投票?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const id = this.carryCurrentRowCode.id;
        getVote(id).then(response => {
          this.form = response.data;
          updateVoteFb(this.form).then(response => {
            this.$modal.msgSuccess("发布成功");
            this.getList();
          });
        });
        this.$router.push({
          path: "/zonghe/drvote",
        });
      });

  },
 isShow(){
  if(this.carryCurrentRowCode.voteState==1){
       this.isShow = true;
       this.getList();

  }
  else {
  this.isShow = false;
    this.getList();
}
    },
    isShow1(){
      if(this.carryCurrentRowCode.voteState==2){
        this.isShow1 = true;
        this.getList();
      }
      else {
        this.isShow1 = false;
        this.getList();
      }
    },
    /** 结束投票按钮 */
    handleUpdateJs() {
      this.$confirm('是否结束投票?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const id = this.carryCurrentRowCode.id;
        getVote(id).then(response => {
          this.form = response.data;
          updateVoteJs(this.form).then(response => {
            this.$modal.msgSuccess("已结束!");
            this.getList();
          });
        });
        this.$router.push({
          path: "/zonghe/drvote",
        });
      });

  },
    /*handleUpdateJs() {
      const id = this.carryCurrentRowCode.id;
      getVote(id).then(response => {
        this.form = response.data;
        updateVoteJs(this.form).then(response => {
          this.$modal.msgSuccess("投票已结束");
          this.getList();
        });
      });
      this.$router.push({
        path: "/zonghe/drvote",
      });
    },*/
    /** 添加投票选项按钮 */
    handleAddd() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addOption(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.getList();
            this.reset();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOption(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOption(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const voteOption = row.voteOption
      this.$modal.confirm('是否确认删除投票选项为"' + voteOption + '"的数据项？').then(function() {
        return delOption(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dr/option/export', {
        ...this.queryParams
      }, `option_${new Date().getTime()}.xlsx`)
    }
  }

};
</script>
